<?php
/**
 * 后台主页控制器
 * User: xuyiping(xuyiping@liangzihuzhu.com)
 * Date: 2017/12/1
 * Time: 14:44
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Model\User\PermissionGroupModel;
use App\Model\User\PermissionModel;
use App\Model\User\RolePermissionModel;
use App\Model\User\UserRoleModel;
use App\Model\User\UserModel;
use Illuminate\Http\Request;

class AdminController extends Controller
{


    /**
     * @param Request $request 请求对象
     * @param int $dashboard 主菜单标识
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function main(Request $request ,$dashboard = 0)
    {

        $sidebar = $request->input('sidebar');
        //用户名
        $userId = session('ADMIN_USER_ID'.session()->getId());

        //查询用户信息
        $user = UserModel::where('userID',$userId)->first();

        //用户信息
        $data['user_id'] = $userId;
        $data['user_name'] = $user->trueName;
        $data['mobile'] = $user->mobile;

        //查询用户角色信息
        $role = UserRoleModel::select('roleID')->find($userId);

        //查询用户角色权限组信息
        $role_permission_group = RolePermissionModel::where('roleID', $role->roleID)->groupBy('permissionGroupID')->get();

        if ($role_permission_group) {
            $role_permission_group = $role_permission_group->pluck('permissionGroupID')->toArray();
        } else {
            $role_permission_group = [];
        }

        $default_url = '';

        //首页链接
        $index_group = [
            'id' => 0,
            'name' => '首页',
            'icon' => '/images/admin/index.png',
            'active' => 0
        ];
        if ($dashboard == 0) {
            $index_group['icon'] =  '/images/admin/index-active.png';
            $index_group['active'] =  1;
            $default_url = 'admin/index';
        }
        $data['permission_group'][] = $index_group;

        //查询权限组信息
        $permission_group = PermissionGroupModel::orderBy('sort', 'asc')->get()->toArray();


        if ($permission_group) {

            foreach ($permission_group as &$group) {

                if (!in_array($group['group_id'], $role_permission_group)) {
                    continue;
                }

                if ($dashboard == $group['group_id']) {
                    $active = 1;
                    //$icon = $group['active_icon'];
                    $data['dashboard'] = $group['group_name'];
                } else {
                    $active = 0;
                    //$icon = $group['icon'];
                }

                $data['permission_group'][] = [
                    'id' => $group['group_id'],
                    'name' => $group['short_name'],
                    //'icon' => $icon,
                    'active' => $active
                ];
            }

        }

        if ($dashboard != 0) {
            //根据选中的权限组id查询对应的角色权限信息
            $user_role_permission = RolePermissionModel::select('permissionID')->where(['roleID' => $role->roleID, 'permissionGroupID' => $dashboard])->get();

            foreach ($user_role_permission as &$role_permission) {

                $permission = PermissionModel::find($role_permission->permissionID);
                if (!$permission || $permission->permissionType == 2) {
                    continue;
                }

                $active = 0;
                if (!empty($sidebar) && strpos($sidebar, $permission->permissionRequest) !== false) {
                    $default_url = $sidebar;
                    $active = 1;
                }

                $data['menus'][] = [
                    'name' => $permission->shortName,
                    'link' => $permission->permissionRequest,
                    'active' => $active
                ];

            }

            if (empty($default_url)) {
                $default_url = $data['menus'][0]['link'];
                $data['menus'][0]['active'] = 1;
            }
        }

        if (strpos($default_url, '/') !== 0) {
            $default_url = '/' . $default_url;
        }
        $data['default_url'] = $default_url;
        $data['pwdFlg'] = $user->pwdFlg;

        return view('Admin/main', $data);
    }

    //首页
    public function index()
    {
        $data = [];
        //  获取当前用户
        $userId = session('ADMIN_USER_ID'.session()->getId());
        return view('Admin/home', $data);
    }

    /**
     * @param Request $request 请求对象
     * @param int $dashboard 主菜单标识
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function dashboard(Request $request ,$dashboard = 0)
    {

        $sidebar = $request->input('sidebar');
        //用户名
        $userId = session('ADMIN_USER_ID' . session()->getId());
        //查询用户信息
        $user = UserModel::where('userID',$userId)->first();

        //用户信息
        $data['user_id'] = $userId;
        $data['user_name'] = $user->trueName;
        $data['mobile'] = $user->mobile;

        //查询用户角色信息
        $role = UserRoleModel::select('roleID')->find($userId);

        //查询用户角色权限组信息
        $role_permission_group = RolePermissionModel::where('roleID', $role->roleID)
            ->groupBy('permissionGroupID')
            ->get();

        if ($role_permission_group) {
            $role_permission_group = $role_permission_group->pluck('permissionGroupID')->toArray();
        } else {
            $role_permission_group = [];
        }

        $default_url = '';

        //首页链接
        $index_group = [
            'id' => 0,
            'name' => '首页',
            'icon' => '/images/admin/index.png',
            'active' => 0
        ];
        if ($dashboard == 0) {
            $index_group['icon'] =  '/images/admin/index-active.png';
            $index_group['active'] =  1;
            $default_url = 'eoa/index';
        }
        $data['permission_group'][] = $index_group;

        //查询权限组信息
        $permission_group = PermissionGroupModel::orderBy('sort', 'asc')->get()->toArray();

        if ($permission_group) {

            foreach ($permission_group as &$group) {

                if (!in_array($group['group_id'], $role_permission_group)) {
                    continue;
                }

                if ($dashboard == $group['group_id']) {
                    $active = 1;
                    //$icon = $group['active_icon'];
                    $data['dashboard'] = $group['group_name'];
                } else {
                    $active = 0;
                    //$icon = $group['icon'];
                }

                $data['permission_group'][] = [
                    'id' => $group['group_id'],
                    'name' => $group['short_name'],
                    //'icon' => $icon,
                    'active' => $active
                ];
            }

        }

        if ($dashboard != 0) {
            //根据选中的权限组id查询对应的角色权限信息
            $user_role_permission = RolePermissionModel::select('permissionID')->where(['roleID' => $role->roleID, 'permissionGroupID' => $dashboard])->get();

            foreach ($user_role_permission as &$role_permission) {

                $permission = PermissionModel::find($role_permission->permissionID);
                if (!$permission || $permission->permissionType == 2) {
                    continue;
                }

                $active = 0;
                if (!empty($sidebar) && strpos($sidebar, $permission->permissionRequest) !== false) {
                    $default_url = $sidebar;
                    $active = 1;
                }

                $data['menus'][] = [
                    'name' => $permission->shortName,
                    'link' => $permission->permissionRequest,
                    'active' => $active
                ];

            }

            if (empty($default_url)) {
                $default_url = $data['menus'][0]['link'];
                $data['menus'][0]['active'] = 1;
            }

        }

        if (strpos($default_url, '/') !== 0) {
            $default_url = '/' . $default_url;
        }

        $data['default_url'] = $default_url;
        $data['pwdFlg'] = $user->pwdFlg;

        return view('Admin/main', $data);

    }

}